<template>
  <div class="mailbox">
    <el-form ref="emailForm" :model="emailForm" :rules="emailRules" class="email-form">
      <h3 class="title">邮箱登录</h3>
      <el-form-item prop="email">
        <el-input v-model="emailForm.email" type="text" auto-complete="off" placeholder="邮箱账号">
          <svg-icon slot="prefix" icon-class="message" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <div v-if="emailNotFound" style="font-size:small; position:relative;bottom:10px;">
        <span style="color:#e40e2e">检测到该邮箱未注册，点击</span><router-link class="link-type" :to="'/register'">注册账号</router-link>
      </div>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input v-model="emailForm.code" auto-complete="off" placeholder="验证码" style="width: 58%"
          @blur="emailForm.code && (emailForm.code = emailForm.code.toString().toUpperCase())">
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <el-button type="primary" size="smail" style="float:right" :disabled="canClick" @click="getEmailCode">{{
          verifyCode }}</el-button>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
          @click.native.prevent="handleMailbox">
          <span v-if="!loading">登 录</span>
          <span v-else>登 陆 中...</span>
        </el-button>
        <div style="float: right;">
          <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-mailbox-footer">
      <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>
  
<script>
import { setEmailCode, EmailLogin } from "@/api/login";

export default {
  name: "Mailbox",
  data() {
    return {
      emailNotFound: false,
      codeUrl: "",
      verifyCode: '发送验证码',
      canClick: false,
      //默认时间
      totalTime: 60,
      emailForm: {
        email: "",
        code: ""
      },
      emailRules: {
        email: [
          { required: true, trigger: "blur", message: "请输入您的邮箱账号" },
          { min: 2, max: 20, message: '邮箱账号长度在 20 之间', trigger: 'blur' }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaEnabled: true
    };
  },

  methods: {
    //获取验证码
    getEmailCode() {
      this.emailNotFound = false;
      //判断邮箱账号是否为空
      if (!this.emailForm.email) {
        this.$message.error("请输入邮箱账号！")
        return
      }
      //判断用户输入的邮箱格式是否正确
      if (!/[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]+/.test(this.emailForm.email)) {
        this.$message.error("请输入正确的邮箱账号！")
        return
      }
      //发送邮箱账号获取验证码
      setEmailCode({ email: this.emailForm.email }).then(res => {
        //请求发送成功 开始倒计时
        this.verifyCode = this.totalTime + "s后重新发送"
        let clock = window.setInterval(() => {
          //倒计时功能
          this.totalTime--
          this.verifyCode = this.totalTime + 's后重新发送'
          if (this.totalTime < 0) {
            window.clearInterval(clock)
            this.verifyCode = '重新获取验证码'
            this.totalTime = 60
            this.canClick = false
          }
        }, 1000)
        this.$message.success("验证码发送成功！");
      })
        .catch(error => {
          this.$message.error(error);
          this.emailNotFound = true;
        })
    },
    //邮箱登录
    handleMailbox() {
      if (!this.emailForm.email) {
        this.$message.error("请输入邮箱账号")
        return
      }
      if (!this.emailForm.code) {
        this.$message.error("请输入验证码")
        return
      }
      const data = {
        email: this.emailForm.email.trim(),
        code: this.emailForm.code,
      }

      this.$store.dispatch("LoginByEmail", data).then((res) => {
        this.$router.push({ path: this.redirect || "/" }).catch(() => { });
      }).catch((e) => {
        this.$modal.msgWarning(e);
        this.loading = false;
      });
    }
  }
};
</script>
  
<style rel="stylesheet/scss" lang="scss">
.mailbox {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.email-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }

  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.el-mailbox-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
</style>
  